package com.top.art.manager;

import com.top.art.bo.ExaminationBO;
import com.top.art.entity.ExaminationInfo;
import com.top.art.entity.ExamineeInfo;
import com.top.art.exception.ArtException;
import com.top.art.service.IExaminationInfoService;
import com.top.art.service.IExamineeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author zhubg
 * @title: ExamineeInfoManager
 * @projectName art-exam-manager
 * @description: TODO
 * @date 2020/10/7-15:04
 */
@Component
public class ExamineeInfoManager {

    @Autowired
    private IExamineeInfoService examineeInfoService;

    @Autowired
    private IExaminationInfoService examinationInfoService;


    public boolean distributionExamination(ExaminationBO bo){

        Map<String, List<ExamineeInfo>> map = examineeInfoService.groupByareaCode(bo);
        List<ExaminationInfo> list = examinationInfoService.groupByareaCode(bo);
//        examinationInfoService.groupByareaCode(bo).stream().forEach(examinationInfo -> {
//            map.forEach((s, examineeInfos) -> {
//                if(examinationInfo.getPeopleCount()>examineeInfos.stream().count()){
//                    System.out.println(examinationInfo.getPeopleCount());
//                    System.out.println(examineeInfos.stream().count());
//                    examineeInfos.stream().forEach();
//                }
//            });
//        });




        //TODO 考场分配逻辑还需要梳理
        try {
            examineeInfoService.groupByareaCode(bo).forEach((s, examineeInfos) -> {
                for(ExamineeInfo info:examineeInfos){
                    examinationInfoService.groupByareaCode(bo).stream().forEach(examinationInfo -> {
                        if(examinationInfo.getPeopleCount()>=examineeInfos.stream().count()){
                            info.setExaminationRoomCode(examinationInfo.getExamCode());
                            examineeInfoService.updateById(info);
                            examineeInfos.remove(examinationInfo);
                        }else {
                            System.out.println("未分配的画室是"+info.toString());
                        }
                    });
                }
            });
//            examinationInfoService.groupByareaCode(bo).forEach((s, n) -> {
//                examineeInfoService.groupByareaCode(bo).forEach((s1, examineeInfos) -> {
//                    System.out.println(n);
//                    System.out.println(examineeInfos);
//                    if(n>examineeInfos.size()){
//                        System.out.println(examineeInfos);
//                        for(ExamineeInfo info:examineeInfos) {
//                            examineeInfos.remove(info);
//                            info.setExaminationRoomCode(s);
//                            examineeInfoService.save(info);
//                        }
//                    }
//                });
//            });
            return true;
        } catch (Exception e) {
            throw new ArtException(5123, "考场分配出错!");
        }
    }


}
